@extends('layouts.default')
@section('content')
    <style>

    </style>

    <div class="container">
        <div class="row row-display row-centered">
            <div class="col-md-12 col-centered">
                <div class="row">
                    <div class="col-xs-12">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <form role="form" method="post" action="{{ route('roles.updatepermission',$id) }}">
                    {{ method_field('PUT') }}
                    {{ csrf_field() }}
                    <div class="box-body" style="display: none;" id="checkboxdiv"></div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <button type="button" class="btn btn-primary pull-left" onclick="getCheckedNodes()">保存</button>
                        <a href="{{ url()->previous() }}" class="btn btn-primary pull-right">返回</a>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <script>
        $(document).ready(function () {
            var setting = {
                view: {
                    showLine: false,
                    showIcon: false,
                    selectedMulti: false,
                    dblClickExpand: false,
                },
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                }
            };
            $.fn.zTree.init($("#treeDemo"), setting, {!! json_encode($permissionTree) !!});
        });

        function getCheckedNodes() {
            $('#checkboxdiv').empty();
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);
            $.each(nodes,function (item,val) {
                $('#checkboxdiv').append('<input type="checkbox" checked name="perms_id[]" value="'+val.id+'">');
            })
            $("form").submit();
        }
    </script>

@stop